package com.fzkj.sort;

import java.util.Arrays;

/**
 * 冒泡排序
 * @author yaya
 */
public class BubbleSort {

    /**
     * 两两比较，交换大小，将最大的移动到数组末尾，重复上述步骤
     */
    public static void main(String[] args) {
        int[] s = {81,94,11,96,12,35,17,95,28,58,41,75,15};
        int[] arr = {9,1,2,5,7,4,8,10,25,11,43,23,6,3,5};
        int[] arr2 = {5,1,4,2,8};
        bubbleSort(arr2);
        System.out.println(Arrays.toString(arr2));
    }

    private static void bubbleSort(int[] arr){
        int tmp;
        for (int i = 0; i < arr.length; i++) {  // 可以从arr.length -- , 已经排序的元素就不参与后续比较
            for (int head = 0; head < arr.length - 1; head++) {
                if (arr[head] > arr[head + 1]) {
                    tmp = arr[head];
                    arr[head] = arr[head + 1];
                    arr[head + 1] = tmp;
                }
            }
            System.out.println("第 "+ (i + 1) +" 轮冒泡排序后结果为 -> " + Arrays.toString(arr));
        }
    }
}
